package edu.fpt.tag;

/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
import edu.fpt.dal.ApartmentDAL;
import edu.fpt.dal.ServicesDAL;
import edu.fpt.dal.UsersDAL;
import edu.fpt.hib.Apartment;
import edu.fpt.hib.RequestServices;
import edu.fpt.hib.Services;
import edu.fpt.hib.Users;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Admin
 */
public class RequestListTag extends SimpleTagSupport {
    private Iterable<RequestServices> list;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        
        try {
            // TODO: insert code to write html before writing the body content.
            // e.g.:
            //
            // out.println("<strong>" + attribute_1 + "</strong>");
            // out.println("    <blockquote>");

            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }
            out.println("<table id=\"mytable\" cellspacing=\"0\">");
                    out.println("<tr>");
                        out.println("<th scope=\"col\" class=\"nobg\">User</th>"+
                        "<th scope=\"col\">At Apartment</th>"+
                        "<th scope=\"col\">Services</th>"+
                        "<th scope=\"col\" colspan=\"2\">Action</th>"+
                    "</tr>");   
                    for (RequestServices req : list) {
                            Users user = UsersDAL.getUsersHQL(req.getUsers().getUserId());
                            Services service = ServicesDAL.getByServicesID(req.getServices().getServiceId());
                            Apartment apart = ApartmentDAL.getApartmentByUserHQL(user.getUserId());
                    
                    out.println("<tr><th class=\"spec\"><a href=\"detailuser.jsp?userid="+user.getUserId()+"\">"+user.getUserId()+"</a></td>"+
                        "<td class=\"spec\"><a href=\"detailapartment.jsp?apartid="+apart.getApartId()+"\">"+apart.getApartId()+"</a></td>"+
                        "<td class=\"spec\"><a href=\"detailservices.jsp?serviceid="+service.getServiceId()+"\">"+service.getServiceName()+"</a></td>"+
                        "<td class=\"spec\">"+
                            "<form method=\"post\" action=\"../ProcessRequest\">"+
                                "<input type=\"hidden\" value=\""+user.getUserId()+"\" name=\"userid\">"+
                                "<input type=\"hidden\" value=\""+service.getServiceId()+"\" name=\"serviceid\">"+
                                "<input type=\"hidden\" value=\"accept\" name=\"action\">"+
                                "<input class=\"searchsubmit\" type=\"submit\" value=\"Approve Request\">"+
                            "</form></td>"+
                    "<td><form method=\"post\" action=\"../ProcessRequest\">"+
                                "<input type=\"hidden\" value=\""+user.getUserId()+"\" name=\"userid\">"+
                                "<input type=\"hidden\" value=\""+service.getServiceId()+"\" name=\"serviceid\">"+
                                "<input type=\"hidden\" value=\"deny\" name=\"action\">"+
                                "<input class=\"searchsubmit\" type=\"submit\" value=\"Reject Request\">"+
                            "</form>"+
                        "</td>"+
                    "</tr>");
                     }
                out.println("</table>");
            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in RequestListTag tag", ex);
        }
    }
}
